package com.cx.mall.goods.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cx.mall.goods.entity.Category;
import com.cx.mall.goods.entity.SkuInfo;
import com.cx.mall.goods.mapper.CategoryMapper;
import com.cx.mall.goods.mapper.SkuInfoMapper;
import com.cx.mall.goods.dto.Product;
import com.cx.mall.goods.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired // 注入分类DAO
    private CategoryMapper categoryMapper;
    @Autowired // 注入商品DAO
    private SkuInfoMapper skuInfoMapper;
    @Override
    public IPage page(Product product) {
        // 商品查询条件构建器
        LambdaQueryChainWrapper<SkuInfo> skuWrapper = new LambdaQueryChainWrapper<>(skuInfoMapper);
        /*
         *  按商品类别查询
         *  如果三级类别不为空，按三级类别查询
         *  否则，如果二级类别不为空，按二级类别查询
         *  否则，如果一级类别不为空，按一级类别查询
         */
        if(product.getCategoryId() != null){
            skuWrapper.eq(SkuInfo::getCategoryId,product.getCategoryId());
        }else if(product.getTwoCategoryId() != null){
            // 1 查询二级类别下的所有三级类别
            // 分类查询条件构建器
            // 提取类别ID
            List<Category> list = getConditionBuilder(product.getTwoCategoryId());
            List<Integer> ids = extractIds(list);
            // 2 构建查询条件
            skuWrapper.in(SkuInfo::getCategoryId,ids);
        }else if(product.getOneCategoryId() != null){
            // 1 查询一级类别下的所有二级类别
            // 分类查询条件构建器
            //List<Category> ones = getConditionBuilder(product.getOneCategoryId());
            // 提取类别ID
            //List<Integer> oneIds = ones.stream().map(Category::getId).collect(Collectors.toList());
            List<Category> ones = getConditionBuilder(product.getOneCategoryId());
            List<Integer> oneIds = extractIds(ones);
            // 2 查询二级类别下的所有三级类别
            // 重建查询条件构造器
            LambdaQueryChainWrapper<Category> twoWrapper = new LambdaQueryChainWrapper<>(categoryMapper);
            List<Category> list = twoWrapper.in(Category::getParentId, oneIds)
                    .select(Category::getId).list();
            // 提取类别ID
            List<Integer> ids = extractIds(list);
            // 3 构建查询条件
            skuWrapper.in(SkuInfo::getCategoryId,ids);
        }
        // 按品牌查询
        if(product.getBrandId() != null){
            skuWrapper.eq(SkuInfo::getBrandId,product.getBrandId());
        }
        // 按商品名称查询
        if(product.getSkuName() != null){
            skuWrapper.like(SkuInfo::getSkuName,product.getSkuName());
        }
        // 封装Page
        Page page = new Page();
        if(product.getCurrent() != null){
            page.setCurrent(product.getCurrent());
        }
        if(product.getSize() != null){
            page.setSize(product.getSize());
        }
        // 选取列
        skuWrapper.select(SkuInfo::getId,SkuInfo::getSkuName,SkuInfo::getPrice,SkuInfo::getSkuDefaultImg);
        return skuWrapper.page(page);
    }
    /**
     * 获取查询条件
     * @param id
     * @return
     */
    private  List<Category> getConditionBuilder(Integer id) {
        LambdaQueryChainWrapper<Category> categoryWrapper = new LambdaQueryChainWrapper<>(categoryMapper);
        List<Category> list = categoryWrapper.eq(Category::getParentId, id)
                .select(Category::getId)
                .list();
        return list;
    }

    /**
     * 提取id
     * @param list
     * @return
     */
    private List<Integer> extractIds(List<Category> list) {
        List<Integer> Listid = list.stream().map(Category::getId).collect(Collectors.toList());
        return Listid;
    }
}